﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Core;

namespace SPHelpCollectionBuilder
{
    public partial class HelpMapBuilder : Form
    {
        public void LoadData(HCHelpMapContainer helpMapContainer)
        {
            if (helpMapContainer != null)
            {
                listBoxHelpMaps.Items.Clear();

                foreach (HelpMap hm in helpMapContainer.HelpMap)
                {
                    listBoxHelpMaps.Items.Add(hm);
                }
            }

            textBoxCollectionId.Text = HCProjectManager.CurrentProject.ProjectManifest.id;
        }

        public HCHelpMapContainer HCHelpMapContainer
        {
            get
            {
                HCHelpMapContainer result = new HCHelpMapContainer();
                if (this.listBoxHelpMaps.Items != null)
                {
                    foreach (object obj in listBoxHelpMaps.Items)
                    {
                        result.HelpMap.Add((HelpMap)obj);
                    }
                }

                return result;
            }
        }

        private void ClearControls()
        {
            textBoxContextId.Text = string.Empty;
            textBoxKey.Text = string.Empty;
            textBoxCollectionId.Text = HCProjectManager.CurrentProject.ProjectManifest.id;
        }

        public HelpMapBuilder()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            //validate input
            if (string.IsNullOrEmpty(textBoxKey.Text))
            {
                MessageBox.Show("Please specify a key", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (string.IsNullOrEmpty(textBoxCollectionId.Text))
            {
                MessageBox.Show("Please specify a collection id", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (string.IsNullOrEmpty(textBoxContextId.Text))
            {
                MessageBox.Show("Please specify a context Id", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (string.Compare(textBoxCollectionId.Text, HCProjectManager.CurrentProject.ProjectManifest.id, false) != 0)
            {
                DialogResult dResult = MessageBox.Show("HelpCollection Id specified here is not this help collection, continue?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

                if (dResult != DialogResult.Yes)
                    return;
            }

            //add this entry
            HelpMap hm = new HelpMap();
            hm.key = textBoxKey.Text;
            hm.collectionId = textBoxCollectionId.Text;
            hm.contextId = textBoxContextId.Text;

            listBoxHelpMaps.Items.Add(hm);

            ClearControls();

            Global.HAS_CHANGES_IN_MEMORY_NOT_SAVED_TO_DISK = true;
        }

        private void listBoxHelpMaps_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnRemove.Enabled = true;
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (listBoxHelpMaps.SelectedItem != null)
            {
                listBoxHelpMaps.Items.RemoveAt(listBoxHelpMaps.SelectedIndex);
                Global.HAS_CHANGES_IN_MEMORY_NOT_SAVED_TO_DISK = true;
            }
        }

        private void buttonHelpContextPicker_Click(object sender, EventArgs e)
        {
            using (TreeViewPicker picker = new TreeViewPicker(new HelpContext()))
            {
                DialogResult dResult = picker.ShowDialog(this);
                if (dResult == DialogResult.OK)
                {
                    textBoxContextId.Text = ((HelpItem)picker.SelectedTreeNodeTag.MirrorObject).id;
                }
            }
        }
    }
}
